
/**
 * 清除空格
 * @export
 * @parmas value
 * @return string
 */
export function clearSpace(value: string | number): string {
  return value ? `${value}`.replace(/\s/g, '') : ''
}

/**
 * 检测是否是中文信息
 * @export
 * @param {string} text
 * @returns {boolean}
 */
export function zhLangDetection(text: string): boolean {
  text = clearSpace(`${text}`)
  const reg = /[\u4E00-\u9FA5\uF900-\uFA2D\!\,\.\，\。\！]/gi
  return reg.test(text) && !text.replaceAll(reg, '').length
}

/**
 * 获取url参数
 * @export
 */
export function getRouteQuery() {
  const params = {}
  const paramList = (`${location.href || ''}`.split('?')[1] || '').split('&')
  paramList.forEach((x) => {
    const [key, value] = x.split('=')
    params[key] = value
  })
  return params
}

/**
 * 是否为空对象
 * @export
 */
export function isEmptyQuote(obj: LooseObject) {
  if (!obj || typeof obj !== 'object') {
    return true
  }
  for (const _ in obj) {
    return false
  }
  return true
}


/**
 * 获取时间差 秒
 * @export
 */
export function getHour(s1: LooseObject, s2: LooseObject) {
  const result1 = s1.split(':')
  const result2 = s2.split(':')

  if (result1.length < 3 && result2.length < 3) {
    const min = result2[0] - result1[0]
    const second = result2[1] - result1[1]
    return min * 60 + second
  } else {
    const hours = result2[0] - result1[0]
    const min = result2[1] - result1[1]
    const second = result2[2] - result1[2]
    return hours * 60 * 60 + min * 60 + second
  }
}


/**
 * 时间范围转时分秒
 * @export
 */
export function formateTime(duration: LooseObject) {
  const list = duration.split("-")
  const time = getHour(list[0], list[1])
  const h = parseInt(time / 3600)
  const minute = parseInt(time / 60 % 60)
  const second = Math.ceil(time % 60)

  const hours = h < 10 ? '0' + h : h
  const formatSecond = second > 59 ? 59 : second
  return `${hours > 0 ? `${hours}:` : ''}${minute < 10 ? '0' + minute : minute}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}`
}

/**
 *秒数转时分秒
 * @export
 */
export function formateSecondTime(duration: LooseObject) {
  const h = parseInt(duration / 3600)
  const minute = parseInt(duration / 60 % 60)
  const second = Math.ceil(duration % 60)

  const hours = h < 10 ? '0' + h : h
  const formatSecond = second > 59 ? 59 : second
  return `${hours > 0 ? `${hours}:` : ''}${minute < 10 ? '0' + minute : minute}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}`
}